//
//  SelectHeadView.m
//  BonweBuy
//
//  Created by liuxin on 2020/2/25.
//  Copyright © 2020 liuxin. All rights reserved.
//

#import "SelectHeadView.h"
#import "SelectColorCollectionViewCell.h"

@interface SelectHeadView ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UIImageView *goodsImageView;      //商品图片
@property (nonatomic, strong) UILabel *priceLabel;                       //售价
@property (nonatomic, strong) UILabel *goodsSkuLabel;                //商品名称
@property (nonatomic, strong) UICollectionView *myCollectionView;  //颜色选择
@end

@implementation SelectHeadView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubViews];
    }
    return self;
}

-(void)createSubViews{
    //商品图片
    UIImageView *goodsImageView = [[UIImageView alloc]init];
    [self addSubview:goodsImageView];
    [goodsImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(kAppWidth_Mini(50));
        make.width.height.mas_equalTo(kAppWidth_Mini(340));
    }];
    self.goodsImageView = goodsImageView;
    
    //价钱
    UILabel *priceLabel = [UILabel initWithTextColor:[UIColor blackColor] fontStr:UIPFRegularFont(15)];
    [self addSubview:priceLabel];
    [priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(goodsImageView.mas_right).offset(kAppWidth_Mini(40));
        make.top.equalTo(goodsImageView);
        make.height.mas_equalTo(kAppWidth_Mini(60));
    }];
    self.priceLabel = priceLabel;
    
    //颜色尺码参数
    UILabel *goodsSkuLabel = [UILabel initWithTextColor:[UIColor blackColor] fontStr:UIPFRegularFont(15)];
    goodsSkuLabel.text = @"请选择颜色和尺码";
    [self addSubview:goodsSkuLabel];
    [goodsSkuLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(goodsImageView.mas_right).offset(kAppWidth_Mini(40));
        make.centerY.equalTo(goodsImageView);
        make.right.mas_equalTo(-kAppWidth_Mini(50));
    }];
    self.goodsSkuLabel = goodsSkuLabel;
    
    //添加分隔线
    [self addLineWithFrame:CGRectMake(0, kAppWidth_Mini(440), SCREEN_WIDTH, 1) color:[UIColor lightGrayColor]];
    
    //颜色标题
    UILabel *colorTitleLabel = [UILabel initWithTextStr:@"颜色:" textColor:[UIColor blackColor] fontStr:UIPFRegularFont(15)];
    [self addSubview:colorTitleLabel];
    [colorTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kAppWidth_Mini(40));
        make.top.mas_equalTo(kAppWidth_Mini(500));
        make.right.mas_equalTo(-kAppWidth_Mini(50));
        make.height.mas_equalTo(kAppWidth_Mini(30));
    }];
    
    //颜色选择
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.sectionInset = UIEdgeInsetsMake(0, kAppWidth_Mini(20), 0, kAppWidth_Mini(-20));
    layout.itemSize = CGSizeMake(kAppWidth_Mini(155), kAppWidth_Mini(130));
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    UICollectionView *myCollectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    myCollectionView.delegate = self;
    myCollectionView.dataSource = self;
    myCollectionView.backgroundColor = [UIColor whiteColor];
    [self addSubview:myCollectionView];
    [myCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(kAppWidth_Mini(130));
        make.top.equalTo(colorTitleLabel.mas_bottom).offset(kAppWidth_Mini(30));
    }];
    [myCollectionView registerClass:[SelectColorCollectionViewCell class] forCellWithReuseIdentifier:@"SelectColorCollectionViewCell"];
    self.myCollectionView = myCollectionView;
}

#pragma mark - UICollectionViewDelegate && DataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.goodsModel.saleAttrList.saleAttr1List.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    SelectColorCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SelectColorCollectionViewCell" forIndexPath:indexPath];
    cell.colorModel = self.goodsModel.saleAttrList.saleAttr1List[indexPath.row];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    self.buyInfoModel.colorModel = self.goodsModel.saleAttrList.saleAttr1List[indexPath.row];
}

#pragma mark - 数据模块赋值
-(void)setGoodsModel:(GoodsDetailModel *)goodsModel{
    _goodsModel = goodsModel;
    [self.goodsImageView sd_setImageWithURL:[NSURL URLWithString:ImageWithURL(_goodsModel.productUrl)]];
    self.priceLabel.text = [NSString stringWithFormat:@"¥%@",[AppTools getStringWithDouble:_goodsModel.salePrice]];
    [self.myCollectionView reloadData];
}

#pragma mark - 购买参数
-(void)setBuyInfoModel:(BuyInfoModel *)buyInfoModel{
    _buyInfoModel = buyInfoModel;
    if (_buyInfoModel.colorModel) {
        [self.goodsImageView sd_setImageWithURL:[NSURL URLWithString:ImageWithURL(_buyInfoModel.colorModel.imageUrl)]];
    }
    if (!_buyInfoModel.colorModel && !_buyInfoModel.sizeModel) {
        self.goodsSkuLabel.text = @"请选择颜色和尺码";
    }else{
        self.goodsSkuLabel.text = [NSString stringWithFormat:@"%@ %@",_buyInfoModel.colorModel?[NSString stringWithFormat:@"%@:%@",_buyInfoModel.colorModel.saleAttr1Key,_buyInfoModel.colorModel.saleAttr1Value]:@"",_buyInfoModel.sizeModel?[NSString stringWithFormat:@"%@:%@",_buyInfoModel.sizeModel.saleAttr2Key,_buyInfoModel.sizeModel.saleAttr2Value]:@""];
    }
}

@end
